########################################################################
# Packages
########################################################################
library(tidyverse)
library(readstata13)
library(foreign)
library(readxl)
library(writexl)

# Set the working directory
setwd()

########################################################################
########################################################################
# Creating the Dataset on Conflict Events
  ### Military Targets
########################################################################
########################################################################

########################################################################
# Datasets
########################################################################

omc_cab <- read_xlsx("CasosAB_202309.xlsx")
#### This is the most updated version of the dataset (09/30/2023). Accessed here: https://micrositios.centrodememoriahistorica.gov.co/observatorio/portal-de-datos/base-de-datos/

glimpse(omc_cab)

# Reshape the dataset

omc_cab <- omc_cab %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

  # Delete unclear categories and events without basic information about year and/or municipality.
omc_cab <- omc_cab %>%
  dplyr::filter(Modalidad != "OTRA ¿CUÁL?")
omc_cab <- omc_cab %>%
  dplyr::filter(year != "0000")
omc_cab <- omc_cab %>%
  dplyr::filter(divipola_alt != "00000")

omc_cab <- omc_cab %>%
  mutate(target = "1. Military")

omc_cab <- omc_cab %>%
  mutate(clash = case_when(
    Modalidad == "COMBATE Y/O CONTACTO ARMADO" ~ 1,
    Modalidad != "COMBATE Y/O CONTACTO ARMADO" ~ 0
  )) # 101. Combate

omc_cab <- omc_cab %>%
  mutate(air_attack = case_when(
    Modalidad == "AMETRALLAMIENTO DESDE EL AIRE" | Modalidad == "BOMBARDEO (ATAQUE AÉREO)" ~ 1,
    Modalidad != "AMETRALLAMIENTO DESDE EL AIRE" & Modalidad != "BOMBARDEO (ATAQUE AÉREO)" ~ 0)) # 102. Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)

omc_cab <- omc_cab %>%
  mutate(mil_operation = case_when(
    Modalidad == "OPERACIÓN MILITAR" ~ 1,
    Modalidad != "OPERACIÓN MILITAR" ~ 0)) # 103. Operativo Militar / Military Operative
  
omc_cab <- omc_cab %>%
  mutate(tatt_mil_target = case_when(
    Modalidad == "ATAQUE A INSTALACIÓN DE LAS FUERZAS ARMADAS ESTATALES" ~ 1,
    Modalidad != "ATAQUE A INSTALACIÓN DE LAS FUERZAS ARMADAS ESTATALES" ~ 0)) # 104. Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar

omc_cab <- omc_cab %>%
  mutate(ambush = case_when(
    Modalidad == "EMBOSCADA" ~ 1,
    Modalidad != "EMBOSCADA" ~ 0)) # 105. Emboscada / Ambush

omc_cab <- omc_cab %>%
  mutate(hitnrun = case_when(
    Modalidad == "HOSTIGAMIENTO" ~ 1,
    Modalidad != "HOSTIGAMIENTO" ~ 0)) # 106. Hostigamiento / Rebel Attack

omc_cab <- omc_cab %>%
  mutate(raid = case_when(
    Modalidad == "ATAQUE A POBLACIÓN" ~ 1,
    Modalidad != "ATAQUE A POBLACIÓN" ~ 0)) # 107. Incursion & Ataque a Población / Rebel Raid

omc_cab <- omc_cab %>%
  mutate(combatant_killed = case_when(
    Modalidad == "COMBATIENTE MUERTO EN CIRCUNSTANCIA NO DETERMINADA" ~ 1,
    Modalidad != "COMBATIENTE MUERTO EN CIRCUNSTANCIA NO DETERMINADA" ~ 0)) # 108. Combatiente Muerto en Circunstancia no Determinada

omc_cab <- omc_cab %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
         )

omc_cab <- omc_cab %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
         )

omc_cab <- omc_cab %>%
  mutate(tatt_civ_target = 0, # Terrorist Attack against Civilian Targets
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

str(omc_cab)

names(omc_cab)[1]<-"id_event"
names(omc_cab)[2]<-"id_related_case"

names(omc_cab)[13]<-"GrupoArmado1"
names(omc_cab)[15]<-"GrupoArmado2"
names(omc_cab)[17]<-"GrupoArmado3"

names(omc_cab)[14]<-"GrupoArmado1_name"
names(omc_cab)[16]<-"GrupoArmado2_name"
names(omc_cab)[18]<-"GrupoArmado3_name"

names(omc_cab)[20]<-"captured_name_group"
names(omc_cab)[21]<-"captured_nonstate"

names(omc_cab)[23]<-"deaths_military"
names(omc_cab)[24]<-"deaths_police"
names(omc_cab)[25]<-"deaths_other_state"
names(omc_cab)[26]<-"deaths_unknown_state"
names(omc_cab)[27]<-"deaths_state_forces"
names(omc_cab)[28]<-"deaths_rebels"
names(omc_cab)[29]<-"deaths_pgm"
names(omc_cab)[30]<-"deaths_gpdp"
names(omc_cab)[31]<-"deaths_unknown_groups"
names(omc_cab)[32]<-"deaths_other_groups"
names(omc_cab)[33]<-"deaths_nonstate_groups"
names(omc_cab)[34]<-"deaths_total_combatants"
names(omc_cab)[35]<-"deaths_total_noinfo"
names(omc_cab)[36]<-"deaths_total_civilians"
names(omc_cab)[37]<-"military_advantage"

# Create temporal variables to reduce complexity
##############################################################################
omc_cab <- omc_cab %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'

omc_cab <- omc_cab %>%
  mutate(del_ga2 = case_when(
    GrupoArmado2 == "AGENTE EXTRANJERO" | GrupoArmado2 == "BANDOLERISMO" |
      GrupoArmado2 == "CRIMEN ORGANIZADO" 
    | GrupoArmado2 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado2 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado2 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado2 == "DESCONOCIDO" | GrupoArmado2 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))

omc_cab <- omc_cab %>%
  mutate(del_ga3 = case_when(
    GrupoArmado3 == "AGENTE EXTRANJERO" | GrupoArmado3 == "BANDOLERISMO" |
      GrupoArmado3 == "CRIMEN ORGANIZADO" 
    | GrupoArmado3 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado3 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado3 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado3 == "DESCONOCIDO" | GrupoArmado3 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))
##############################################################################

# Split and later merge again
omc_cab_clash <- omc_cab %>%
  filter(clash == 1)

omc_cab_clash <- omc_cab_clash %>%
  mutate(event_type = "1. Clash")

omc_cab_clash <- omc_cab_clash %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" | GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" | GrupoArmado2 == "AGENTE DEL ESTADO" | GrupoArmado2 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" | GrupoArmado3 == "AGENTE DEL ESTADO" | GrupoArmado3 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" ~ 1,
    GrupoArmado1 != "AGENTE DEL ESTADO" & GrupoArmado1 != "AGENTE DEL ESTADO - GRUPO PARAMILITAR" & GrupoArmado2 != "AGENTE DEL ESTADO" & GrupoArmado2 != "AGENTE DEL ESTADO - GRUPO PARAMILITAR" & GrupoArmado3 != "AGENTE DEL ESTADO" & GrupoArmado3 != "AGENTE DEL ESTADO - GRUPO PARAMILITAR" ~ 0)
  )

omc_cab_clash <- omc_cab_clash %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA" ~ 1,
    GrupoArmado1 != "GUERRILLA" & GrupoArmado2 != "GUERRILLA" & GrupoArmado3 != "GUERRILLA"  ~ 0))

omc_cab_clash <- omc_cab_clash %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR" 
    | GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" | GrupoArmado2 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" | GrupoArmado3 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" ~ 1,
    GrupoArmado1 != "GRUPO PARAMILITAR" & GrupoArmado2 != "GRUPO PARAMILITAR" & GrupoArmado3 != "GRUPO PARAMILITAR" 
    & GrupoArmado1 != "AGENTE DEL ESTADO - GRUPO PARAMILITAR" & GrupoArmado2 != "AGENTE DEL ESTADO - GRUPO PARAMILITAR" & GrupoArmado3 != "AGENTE DEL ESTADO - GRUPO PARAMILITAR" ~ 0))

omc_cab_clash <- omc_cab_clash %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" | del_ga2 == "Other Group" 
    | del_ga3 == "Other Group" ~ 1,
    del_ga1 != "Other Group" & del_ga2 != "Other Group" 
    & del_ga3 != "Other Group" ~ 0))

omc_cab_clash <- omc_cab_clash %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" 
    | del_ga3 == "Unidentified Group" ~ 1,
    del_ga1 != "Unidentified Group" & del_ga2 != "Unidentified Group" 
    & del_ga3 != "Unidentified Group" ~ 0))

omc_cab_clash <- omc_cab_clash %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" | del_ga2 == "Unknown" 
    | del_ga3 == "Unknown" ~ 1,
    del_ga1 != "Unknown" & del_ga2 != "Unknown" 
    & del_ga3 != "Unknown" ~ 0))

omc_cab_clash <- omc_cab_clash %>%
  mutate(clash_2 = 1 
  ) ## This version of clash captures complex events in which there was presumably a clash and a unilateral action (that triggered the event, such as air attack and state forces deaths)

omc_cab_clash <- omc_cab_clash %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, 
                 other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) ## Because in the process some NAs are created. But it is correct to assign 0.


# Split and later merge again
omc_cab_air_attack <- omc_cab %>%
  filter(air_attack == 1)

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(event_type = case_when(
    deaths_state_forces > 0 ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" ~ 1,
    GrupoArmado1 != "AGENTE DEL ESTADO" ~ 0))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(rebel_groups = case_when(
    event_type == "3. Complex Event" &
      (GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
    TRUE ~ 0))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(progov_militias = case_when(
    event_type == "3. Complex Event" &
      (GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR") ~ 1,
    TRUE ~ 0))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(other_ag = case_when(
    event_type == "3. Complex Event" &
      (del_ga1 == "Other Group" | del_ga2 == "Other Group" 
       | del_ga3 == "Other Group") ~ 1,
    TRUE ~ 0))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(unident_ag = case_when(
    event_type == "3. Complex Event" &
      (del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" 
       | del_ga3 == "Unidentified Group") ~ 1,
    TRUE ~ 0))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(unknown = case_when(
    event_type == "3. Complex Event" &
      (del_ga1 == "Unknown" | del_ga2 == "Unknown" 
       | del_ga3 == "Unknown") ~ 1,
    TRUE ~ 0))

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, 
                 other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

omc_cab_air_attack <- omc_cab_air_attack %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

# Split and later merge again
omc_cab_mil_operation <- omc_cab %>%
  filter(mil_operation == 1)

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(event_type = case_when(
    deaths_state_forces > 0 ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" ~ 1,
    TRUE ~ 0)) # I checked that this variable would be the same as GA1 == Agentes del Estado | Agentes del Estado - Grupo Paramilitary

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(rebel_groups = case_when(
    event_type == "3. Complex Event" &
      (GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
    TRUE ~ 0))

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(progov_militias = case_when(
    event_type == "3. Complex Event" &
      (GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR") ~ 1,
    TRUE ~ 0))

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(other_ag = case_when(
    event_type == "3. Complex Event" &
      (del_ga1 == "Other Group" | del_ga2 == "Other Group" 
       | del_ga3 == "Other Group") ~ 1,
    TRUE ~ 0))

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(unident_ag = case_when(
    event_type == "3. Complex Event" &
      (del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" 
       | del_ga3 == "Unidentified Group") ~ 1,
    TRUE ~ 0))

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(unknown = case_when(
    event_type == "3. Complex Event" &
      (del_ga1 == "Unknown" | del_ga2 == "Unknown" 
       | del_ga3 == "Unknown") ~ 1,
    TRUE ~ 0))

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) ## Useful to replace with 0s only by selected columns

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

# Split and later merge again
omc_cab_tatt_mil_target <- omc_cab %>%
  filter(tatt_mil_target == 1)

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(event_type = case_when(
    deaths_nonstate_groups > 0 ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(state_forces = case_when(
    event_type == "3. Complex Event" &
      (GrupoArmado1 == "AGENTE DEL ESTADO" | GrupoArmado2 == "AGENTE DEL ESTADO" | GrupoArmado3 == "AGENTE DEL ESTADO") ~ 1,
    TRUE ~ 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR" ~ 1,
    TRUE ~ 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" | del_ga2 == "Other Group" 
    | del_ga3 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" 
    | del_ga3 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" | del_ga2 == "Unknown" 
    | del_ga3 == "Unknown" ~ 1,
    TRUE ~ 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0))

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

# Split and later merge again
omc_cab_ambush <- omc_cab %>%
  filter(ambush == 1)

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(event_type = case_when(
    (Iniciativa != "FUERZAS ARMADAS ESTATALES" &
       deaths_nonstate_groups > 0) | 
      (Iniciativa == "FUERZAS ARMADAS ESTATALES" &
         deaths_state_forces > 0)  ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(state_forces = case_when(
    (Iniciativa == "FUERZAS ARMADAS ESTATALES") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & 
          event_type == "3. Complex Event") & 
         (GrupoArmado1 == "AGENTE DEL ESTADO" | GrupoArmado2 == "AGENTE DEL ESTADO" | GrupoArmado3 == "AGENTE DEL ESTADO")) ~ 1,
    TRUE ~ 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(rebel_groups = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GUERRILLA") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) ~ 1,
    TRUE ~ 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(progov_militias = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GRUPO PARAMILITAR") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) ~ 1,
    TRUE ~ 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(other_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Other Group" | del_ga2 == "Other Group" | del_ga3 == "Other Group")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Other Group") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Other Group" | del_ga3 == "Other Group")) ~ 1,
    TRUE ~ 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(unident_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unidentified Group") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) ~ 1,
    TRUE ~ 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(unknown = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unknown" | del_ga2 == "Unknown" | del_ga3 == "Unknown")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unknown") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unknown" | del_ga3 == "Unknown")) ~ 1,
    TRUE ~ 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0))

omc_cab_ambush <- omc_cab_ambush %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

# Split and later merge again
omc_cab_hitnrun <- omc_cab %>%
  filter(hitnrun == 1)

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(event_type = case_when(
    (Iniciativa != "FUERZAS ARMADAS ESTATALES" &
       deaths_nonstate_groups > 0) | 
      (Iniciativa == "FUERZAS ARMADAS ESTATALES" &
         deaths_state_forces > 0)  ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(state_forces = case_when(
    (Iniciativa == "FUERZAS ARMADAS ESTATALES") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & 
          event_type == "3. Complex Event") & 
         (GrupoArmado1 == "AGENTE DEL ESTADO" | GrupoArmado2 == "AGENTE DEL ESTADO" | GrupoArmado3 == "AGENTE DEL ESTADO")) ~ 1,
    TRUE ~ 0))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(rebel_groups = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GUERRILLA") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) ~ 1,
    TRUE ~ 0))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(progov_militias = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GRUPO PARAMILITAR") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) ~ 1,
    TRUE ~ 0))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(other_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Other Group" | del_ga2 == "Other Group" | del_ga3 == "Other Group")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Other Group") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Other Group" | del_ga3 == "Other Group")) ~ 1,
    TRUE ~ 0))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(unident_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unidentified Group") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) ~ 1,
    TRUE ~ 0))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(unknown = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unknown" | del_ga2 == "Unknown" | del_ga3 == "Unknown")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unknown") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unknown" | del_ga3 == "Unknown")) ~ 1,
    TRUE ~ 0))

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

# Split and later merge again
omc_cab_raid <- omc_cab %>%
  filter(raid == 1)

omc_cab_raid <- omc_cab_raid %>%
  mutate(event_type = case_when(
    (Iniciativa != "FUERZAS ARMADAS ESTATALES" &
       deaths_nonstate_groups > 0) | 
      (Iniciativa == "FUERZAS ARMADAS ESTATALES" &
         deaths_state_forces > 0)  ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cab_raid <- omc_cab_raid %>%
  mutate(state_forces = case_when(
    (Iniciativa == "FUERZAS ARMADAS ESTATALES") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & 
          event_type == "3. Complex Event") & 
         (GrupoArmado1 == "AGENTE DEL ESTADO" | GrupoArmado2 == "AGENTE DEL ESTADO" | GrupoArmado3 == "AGENTE DEL ESTADO")) ~ 1,
    TRUE ~ 0))

omc_cab_raid <- omc_cab_raid %>%
  mutate(rebel_groups = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GUERRILLA") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) ~ 1,
    TRUE ~ 0))

omc_cab_raid <- omc_cab_raid %>%
  mutate(progov_militias = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GRUPO PARAMILITAR") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) ~ 1,
    TRUE ~ 0))

omc_cab_raid <- omc_cab_raid %>%
  mutate(other_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Other Group" | del_ga2 == "Other Group" | del_ga3 == "Other Group")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Other Group") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Other Group" | del_ga3 == "Other Group")) ~ 1,
    TRUE ~ 0))

omc_cab_raid <- omc_cab_raid %>%
  mutate(unident_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unidentified Group") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) ~ 1,
    TRUE ~ 0))

omc_cab_raid <- omc_cab_raid %>%
  mutate(unknown = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unknown" | del_ga2 == "Unknown" | del_ga3 == "Unknown")) |
      (Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unknown") |
      ((Iniciativa != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unknown" | del_ga3 == "Unknown")) ~ 1,
    TRUE ~ 0))

omc_cab_raid <- omc_cab_raid %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

omc_cab_raid <- omc_cab_raid %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

###########################################################
# Reshape the dataset - AP
###########################################################

omc_cap <- read_xlsx("CasosAP_202309.xlsx")
#### This is the most updated version of the dataset (09/30/2023). Accessed here: https://micrositios.centrodememoriahistorica.gov.co/observatorio/portal-de-datos/base-de-datos/

omc_cap <- omc_cap %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                mun_uc = Municipio,
                dep_uc = Departamento)

names(omc_cap)[1]<-"id_event"
names(omc_cap)[2]<-"id_related_case"

names(omc_cap)[6]<-"divipola_alt"
names(omc_cap)[10]<-"GrupoArmado1"
names(omc_cap)[12]<-"GrupoArmado2"
names(omc_cap)[14]<-"GrupoArmado3"

names(omc_cap)[11]<-"GrupoArmado1_name"
names(omc_cap)[13]<-"GrupoArmado2_name"
names(omc_cap)[15]<-"GrupoArmado3_name"

names(omc_cap)[27]<-"captured_name_group"
names(omc_cap)[28]<-"captured_nonstate"

names(omc_cap)[31]<-"deaths_military"
names(omc_cap)[32]<-"deaths_police"
names(omc_cap)[33]<-"deaths_other_state"
names(omc_cap)[34]<-"deaths_unknown_state"
names(omc_cap)[35]<-"deaths_state_forces"

names(omc_cap)[36]<-"deaths_rebels"
names(omc_cap)[37]<-"deaths_pgm"
names(omc_cap)[38]<-"deaths_gpdp"
names(omc_cap)[39]<-"deaths_unknown_groups"
names(omc_cap)[40]<-"deaths_other_groups"
names(omc_cap)[41]<-"deaths_nonstate_groups"
names(omc_cap)[42]<-"deaths_total_combatants"
names(omc_cap)[43]<-"deaths_total_noinfo"
names(omc_cap)[44]<-"deaths_total_civilians"
names(omc_cap)[45]<-"military_advantage"

omc_cap$divipola_alt <- as.character(omc_cap$divipola_alt)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cap <- omc_cap %>%
  dplyr::filter(year != "0000")
omc_cap <- omc_cap %>%
  dplyr::filter(divipola_alt != "00000")

omc_cap <- omc_cap %>%
  mutate(target = "1. Military")

omc_cap <- omc_cap %>%
  mutate(clash = 0, # 
         air_attack = 0, # 
         mil_operation = 0, # 
         tatt_mil_target = 0, # 
         ambush = 0, # 
         hitnrun = 0, #
         raid = 1, # 
         combatant_killed = 0
  )

omc_cap <- omc_cap %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Ataque Terrorista con Objetivos Civiles
  )

omc_cap <- omc_cap %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Ataque Terrorista con Objetivos Ambiguo
  )

##############################################################################
# Create temporal variables to reduce complexity
##############################################################################
omc_cap <- omc_cap %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))

omc_cap <- omc_cap %>%
  mutate(del_ga2 = case_when(
    GrupoArmado2 == "AGENTE EXTRANJERO" | GrupoArmado2 == "BANDOLERISMO" |
      GrupoArmado2 == "CRIMEN ORGANIZADO" 
    | GrupoArmado2 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado2 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado2 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado2 == "DESCONOCIDO" | GrupoArmado2 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))

omc_cap <- omc_cap %>%
  mutate(del_ga3 = case_when(
    GrupoArmado3 == "AGENTE EXTRANJERO" | GrupoArmado3 == "BANDOLERISMO" |
      GrupoArmado3 == "CRIMEN ORGANIZADO" 
    | GrupoArmado3 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado3 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado3 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado3 == "DESCONOCIDO" | GrupoArmado3 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))
##############################################################################
  
  # It is assumed that the initiative of these actions is by nonstate armed groups. Unfortunately, the dataset lacks the variable, initiative.
  # To deal with this problem, I will use military advantage 'Ventaja Militar' as a proxy of initiative. 

omc_cap <- omc_cap %>%
  mutate(event_type = case_when(
    (military_advantage != "FUERZAS ARMADAS ESTATALES" &
       deaths_nonstate_groups > 0) | 
      (military_advantage == "FUERZAS ARMADAS ESTATALES" &
         deaths_state_forces > 0)  ~ "3. Complex Event",
    TRUE ~ "2. Unilateral Action"))

omc_cap <- omc_cap %>%
  mutate(state_forces = case_when(
    (military_advantage == "FUERZAS ARMADAS ESTATALES") |
      ((military_advantage != "FUERZAS ARMADAS ESTATALES" & 
          event_type == "3. Complex Event") & 
         (GrupoArmado1 == "AGENTE DEL ESTADO" | GrupoArmado2 == "AGENTE DEL ESTADO" | GrupoArmado3 == "AGENTE DEL ESTADO")) ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate(rebel_groups = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GUERRILLA" | GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) |
      (military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GUERRILLA") |
      ((military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA")) ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate(progov_militias = case_when(
    (event_type == "3. Complex Event" & 
       (GrupoArmado1 == "GRUPO PARAMILITAR" | GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) |
      (military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "GRUPO PARAMILITAR") |
      ((military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (GrupoArmado2 == "GRUPO PARAMILITAR" | GrupoArmado3 == "GRUPO PARAMILITAR")) ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate(other_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Other Group" | del_ga2 == "Other Group" | del_ga3 == "Other Group")) |
      (military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Other Group") |
      ((military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Other Group" | del_ga3 == "Other Group")) ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate(unident_ag = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unidentified Group" | del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) |
      (military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unidentified Group") |
      ((military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unidentified Group" | del_ga3 == "Unidentified Group")) ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate(unknown = case_when(
    (event_type == "3. Complex Event" & 
       (del_ga1 == "Unknown" | del_ga2 == "Unknown" | del_ga3 == "Unknown")) |
      (military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & del_ga1 == "Unknown") |
      ((military_advantage != "FUERZAS ARMADAS ESTATALES" & event_type == "2. Unilateral Action" & GrupoArmado1 == "AGENTE DEL ESTADO") & (del_ga2 == "Unknown" | del_ga3 == "Unknown")) ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

omc_cap <- omc_cap %>%
  mutate(clash_2 = case_when(
    event_type == "1. Clash" | event_type == "3. Complex Event" ~ 1,
    TRUE ~ 0))

omc_cap <- omc_cap %>%
  mutate(Iniciativa = NA)

################################################################
# To complete the list of events related to military targets, I merge the 'Acciones Bélicas' and 'Ataques a Poblados' datasets with the events of terrorist attacks that targeted military objectives from the 'Acciones Terroristas' dataset. 
################################################################

omc_cat <- read_xlsx("CasosAT_202309.xlsx")

# Reshape the dataset

omc_cat <- omc_cat %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                mun_uc = Municipio,
                dep_uc = Departamento)

names(omc_cat)[1]<-"id_event"
names(omc_cat)[2]<-"id_related_case"

names(omc_cat)[6]<-"divipola_alt"
names(omc_cat)[11]<-"GrupoArmado1"
names(omc_cat)[24]<-"deaths_total_combatants"
names(omc_cat)[25]<-"deaths_total_civilians"

names(omc_cat)[12]<-"GrupoArmado1_name"

omc_cat$divipola_alt <- as.character(omc_cat$divipola_alt)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cat <- omc_cat %>%
  dplyr::filter(year != "0000")
omc_cat <- omc_cat %>%
  dplyr::filter(divipola_alt != "00000")

omc_cat_mobj <- omc_cat %>%
  dplyr::filter(Modalidad == "ATAQUE A OBJETIVO MILITAR CON EFECTO INDISCRIMINADO")

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(target = "1. Military")

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(event_type = "2. Unilateral Action")

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(clash = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 1, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0, # Incursion y Ataque a Población
         combatant_killed = 0, # Combatiente Muerto en Circunstancia no Determinada 
         clash_2 = 0)

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0, # Ataque Terrorista contra Objetivo Civil
  )

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0, # Ataque Terrorista contra Objetivo Ambiguo
  )

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado1_name = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

# Create temporal variables to reduce complexity
##############################################################################
omc_cat_mobj <- omc_cat_mobj %>%
  mutate(del_ga = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO"
    | GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))
##############################################################################

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(state_forces = 0)

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" ~ 1,
    TRUE ~ 0))

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(other_ag = case_when(
    del_ga == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(unident_ag = case_when(
    del_ga == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cat_mobj <- omc_cat_mobj %>%
  mutate(unknown = case_when(
    del_ga == "Unknown" ~ 1,
    TRUE ~ 0))

omc_cat_mobj <- omc_cat_mobj %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

################################################################
# Now, I will merge all the datasets related to military targets.
################################################################

glimpse(omc_cab_clash)

omc_cab_clash <- omc_cab_clash %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cab_air_attack <- omc_cab_air_attack %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cab_mil_operation <- omc_cab_mil_operation %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cab_tatt_mil_target <- omc_cab_tatt_mil_target %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cab_ambush <- omc_cab_ambush %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cab_hitnrun <- omc_cab_hitnrun %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cab_raid <- omc_cab_raid %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cap <- omc_cap %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cat_mobj <- omc_cat_mobj %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

# To check consistency in relation to the merged dataset
omc_cab_2 <- omc_cab %>%
  filter(Modalidad != "COMBATIENTE MUERTO EN CIRCUNSTANCIA NO DETERMINADA")

omc_cab_n <- dplyr::bind_rows(omc_cab_clash, omc_cab_air_attack)
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cab_mil_operation)
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cab_tatt_mil_target)
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cab_ambush)
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cab_hitnrun)
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cab_raid)
# Comparing omc_cab_2 and omc_cab_n yields the same number of observations -> they agree (04/20/2024) ## And double-checked 09/13/2024
# Merge the other two datasets ('AP' and 'AT-MilitaryObjective')
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cap)
omc_cab_n <- dplyr::bind_rows(omc_cab_n, omc_cat_mobj)

omc_cab_n_dep <- omc_cab_n %>% distinct(id_event, year, month, day, divipola_alt, .keep_all = TRUE)

omc_cab_n_depurated <- omc_cab_n %>% distinct(year, month, day, divipola_alt, .keep_all = TRUE)

########################################################################
# Further transformations: actions by armed groups
########################################################################

write_csv(omc_cab_n, "OMC-CasosFinal_MilitaryTarget-Updated(04242024).csv") # Deprecated

########################################################################
### Actions that are exclusive of the state forces, according to OMC- CNMH:
  # Ametrallamientos -> sf_air_attacks
  # Bombardeo -> sf_air_attacks
  # Operación militar -> sf_mil_operations
### Actions that are exclusive of rebel groups, according to OMC- CNMH:
  # Ataques a instalaciones de la FP -> rg_tatt_mil_targets
### Actions that can apply to both sides or more ambiguous, according to OMC- CNMH:
  # Embocada -> sf_ambushes
  # Hostigamientos -> sf_hitnruns
  # Bloqueo de vías
########################################################################

## I will prepare two versions of the aggregated version: the old version and the new version ## ## Delete Now ##

# Now, I will create the actions that are presumably perpetrated by the state
omc_cab_n <- omc_cab_n %>%
    mutate(
    sf_air_attacks = case_when(
      state_forces == 1 & air_attack == 1 ~ 1,
      TRUE ~ 0),
    sf_mil_operations = case_when(
      state_forces == 1 & mil_operation == 1 ~ 1,
      TRUE ~ 0),
    sf_ambushes =  case_when(
        ambush == 1 & Iniciativa == "FUERZAS ARMADAS ESTATALES" & state_forces == 1 ~ 1,
      TRUE ~ 0),
    sf_hitnruns =  case_when(
      hitnrun == 1 & Iniciativa == "FUERZAS ARMADAS ESTATALES" & state_forces == 1 ~ 1,
      TRUE ~ 0),
    sf_raids =  case_when(
      (raid == 1 & Iniciativa == "FUERZAS ARMADAS ESTATALES" & state_forces == 1) |
        (raid == 1 & military_advantage == "FUERZAS ARMADAS ESTATALES" & state_forces == 1)  ~ 1,
      TRUE ~ 0))

# Now, I will only count those state actions against rebel organizations
omc_cab_n <- omc_cab_n %>%
  mutate(
    sf_air_attacks_xr = case_when(
      sf_air_attacks == 1 & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
      TRUE ~ 0),
    sf_mil_operations_xr = case_when(
      sf_mil_operations == 1 & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
      TRUE ~ 0),
    sf_ambushes_xr =  case_when(
      sf_ambushes == 1 & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
      TRUE ~ 0),
    sf_hitnruns_xr =  case_when(
      sf_hitnruns == 1 & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
      TRUE ~ 0),
    sf_raids_xr =  case_when(
      sf_raids == 1 & (GrupoArmado2 == "GUERRILLA" | GrupoArmado3 == "GUERRILLA") ~ 1,
      TRUE ~ 0))

# Now, I will create the actions that are presumably perpetrated by rebel groups
omc_cab_n <- omc_cab_n %>%
  mutate(
    rg_tatt_mil_targets =  case_when(
      tatt_mil_target == 1 & rebel_groups == 1 ~ 1,
      TRUE ~ 0),
    rg_ambushes =  case_when(
      ambush == 1 & Iniciativa == "GRUPOS ARMADOS ORGANIZADOS" & 
        rebel_groups == 1 & (progov_militias == 0 | progov_militias == 1 & deaths_pgm > 0) ~ 1,
      TRUE ~ 0),
    rg_hitnruns =  case_when(
      hitnrun == 1 & Iniciativa == "GRUPOS ARMADOS ORGANIZADOS" & 
        rebel_groups == 1 & (progov_militias == 0 | progov_militias == 1 & deaths_pgm > 0) ~ 1,
      TRUE ~ 0),
    rg_raids =  case_when(
      (raid == 1 & Iniciativa == "GRUPOS ARMADOS ORGANIZADOS" & 
        rebel_groups == 1 & (progov_militias == 0 | progov_militias == 1 & deaths_pgm > 0)) |
        (raid == 1 & military_advantage == "GRUPOS ARMADOS ORGANIZADOS" & 
           rebel_groups == 1 & (progov_militias == 0 | progov_militias == 1 & deaths_pgm > 0))  ~ 1,
      TRUE ~ 0))

# Now, I will only count those rebel actions against state forces
omc_cab_n <- omc_cab_n %>%
  mutate(
    rg_tatt_mil_targets_xs =  case_when(
      rg_tatt_mil_targets == 1 ~ 1,
      TRUE ~ 0),
    rg_ambushes_xs =  case_when(
      rg_ambushes == 1 & (GrupoArmado1 == "AGENTE DEL ESTADO" | 
                            GrupoArmado2 == "AGENTE DEL ESTADO" | 
                            GrupoArmado3 == "AGENTE DEL ESTADO") ~ 1,
      TRUE ~ 0),
    rg_hitnruns_xs =  case_when(
      rg_hitnruns == 1 & (GrupoArmado1 == "AGENTE DEL ESTADO" | 
                            GrupoArmado2 == "AGENTE DEL ESTADO" | 
                            GrupoArmado3 == "AGENTE DEL ESTADO") ~ 1,
      TRUE ~ 0),
    rg_raids_xs =  case_when(
      rg_raids == 1 & (GrupoArmado1 == "AGENTE DEL ESTADO" | 
                         GrupoArmado2 == "AGENTE DEL ESTADO" | 
                         GrupoArmado3 == "AGENTE DEL ESTADO") ~ 1,
      TRUE ~ 0))

# Now, I will create the clashes between rebels and state forces
omc_cab_n <- omc_cab_n %>%
  mutate(
    cl_st_rg =  case_when(
      clash == 1 & state_forces == 1 & rebel_groups == 1 ~ 1,
      TRUE ~ 0),
    cl_st_rg_2 =  case_when(
      clash_2 == 1 & state_forces == 1 & rebel_groups == 1 ~ 1,
      TRUE ~ 0))

write_csv(omc_cab_n, "~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)-OMC-CasosFinal_MilitaryTarget-Updated(09132024).csv")

omc_cab_n <- read_csv("~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)-OMC-CasosFinal_MilitaryTarget-Updated(09132024).csv")

rm(list=setdiff(ls(), "omc_cab_n"))

########################################################################
########################################################################
# Creating the Dataset on Conflict Events
### Civilian Targets
########################################################################
########################################################################

omc_cas <- read_xlsx("CasosAS_202309.xlsx")

# Reshape the dataset

omc_cas <- omc_cas %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cas <- omc_cas %>%
  dplyr::filter(year != 0)
omc_cas <- omc_cas %>%
  dplyr::filter(divipola_alt != "00000")

omc_cas <- omc_cas %>%
  mutate(target = "2. Civilian")

omc_cas <- omc_cas %>%
  mutate(event_type = "2. Unilateral Action")

omc_cas <- omc_cas %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cas <- omc_cas %>%
  mutate(assassination = 1, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cas <- omc_cas %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

names(omc_cas)[1]<-"id_event"
names(omc_cas)[2]<-"id_related_case"

names(omc_cas)[11]<-"GrupoArmado1"
names(omc_cas)[12]<-"GrupoArmado1_name"

omc_cas <- omc_cas %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         deaths_total_combatants = NA,
         deaths_total_civilians = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cas <- omc_cas %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cas <- omc_cas %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cas <- omc_cas %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cas <- omc_cas %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cas <- omc_cas %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cas <- omc_cas %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cas <- omc_cas %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

### Massacres ###
omc_cma <- read_xlsx("CasosMA_202309.xlsx")

# Reshape the dataset

omc_cma <- omc_cma %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cma <- omc_cma %>%
  dplyr::filter(year != 0)
omc_cma <- omc_cma %>%
  dplyr::filter(divipola_alt != "00000")

omc_cma <- omc_cma %>%
  mutate(target = "2. Civilian")

omc_cma <- omc_cma %>%
  mutate(event_type = "2. Unilateral Action")

omc_cma <- omc_cma %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cma <- omc_cma %>%
  mutate(assassination = 0, # Asesinato
         massacre = 1, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cma <- omc_cma %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

names(omc_cma)[1]<-"id_event"
names(omc_cma)[2]<-"id_related_case"

names(omc_cma)[11]<-"GrupoArmado1"
names(omc_cma)[12]<-"GrupoArmado1_name"

omc_cma <- omc_cma %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         deaths_total_combatants = NA,
         deaths_total_civilians = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cma <- omc_cma %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cma %>%
  group_by(GrupoArmado1) %>%
  count()

omc_cma <- omc_cma %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cma <- omc_cma %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cma <- omc_cma %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cma <- omc_cma %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cma <- omc_cma %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cma <- omc_cma %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

### Forced Disappereances ###

omc_cdf <- read_xlsx("CasosDF_202309.xlsx")

# Reshape the dataset

omc_cdf <- omc_cdf %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cdf <- omc_cdf %>%
  dplyr::filter(year != 0)
omc_cdf <- omc_cdf %>%
  dplyr::filter(divipola_alt != "00000")

omc_cdf <- omc_cdf %>%
  mutate(target = "2. Civilian")

omc_cdf <- omc_cdf %>%
  mutate(event_type = "2. Unilateral Action")

omc_cdf <- omc_cdf %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cdf <- omc_cdf %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 1, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cdf <- omc_cdf %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

names(omc_cdf)[1]<-"id_event"
names(omc_cdf)[2]<-"id_related_case"

names(omc_cdf)[11]<-"GrupoArmado1"
names(omc_cdf)[12]<-"GrupoArmado1_name"

omc_cdf <- omc_cdf %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         deaths_total_combatants = NA,
         deaths_total_civilians = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cdf <- omc_cdf %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cdf %>%
  group_by(GrupoArmado1) %>%
  count()

omc_cdf <- omc_cdf %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cdf <- omc_cdf %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cdf <- omc_cdf %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cdf <- omc_cdf %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cdf <- omc_cdf %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cdf <- omc_cdf %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

### Kidnapping ###

omc_cse <- read_xlsx("CasosSE_202309.xlsx")

# Reshape the data

omc_cse <- omc_cse %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cse <- omc_cse %>%
  dplyr::filter(year != 0)
omc_cse <- omc_cse %>%
  dplyr::filter(divipola_alt != "00000")

omc_cse <- omc_cse %>%
  mutate(target = "2. Civilian")

omc_cse <- omc_cse %>%
  mutate(event_type = "2. Unilateral Action")

names(omc_cse)[1]<-"id_event"
names(omc_cse)[2]<-"id_related_case"

names(omc_cse)[11]<-"GrupoArmado1"
names(omc_cse)[12]<-"GrupoArmado1_name"

names(omc_cse)[25]<-"KidnappingType"


omc_cse <- omc_cse %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cse <- omc_cse %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cse <- omc_cse %>%
  mutate(ind_kidn = case_when(
    KidnappingType == "INDIVIDUAL" ~ 1,
    TRUE ~ 0
  ))

omc_cse <- omc_cse %>%
  mutate(col_kidn = case_when(
    KidnappingType == "MÚLTIPLE" |
      KidnappingType == "COLECTIVO" ~ 1,
    TRUE ~ 0
  ))

omc_cse <- omc_cse %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

omc_cse <- omc_cse %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         deaths_total_combatants = NA,
         deaths_total_civilians = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cse <- omc_cse %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cse <- omc_cse %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cse <- omc_cse %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cse <- omc_cse %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cse <- omc_cse %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cse <- omc_cse %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cse <- omc_cse %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

### Sexual Violence ###

omc_cvs <- read_xlsx("CasosVS_202309.xlsx")

# Reshape the dataset

omc_cvs <- omc_cvs %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cvs <- omc_cvs %>%
  dplyr::filter(year != 0)
omc_cvs <- omc_cvs %>%
  dplyr::filter(divipola_alt != "00000")

omc_cvs <- omc_cvs %>%
  mutate(target = "2. Civilian")

omc_cvs <- omc_cvs %>%
  mutate(event_type = "2. Unilateral Action")

omc_cvs <- omc_cvs %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cvs <- omc_cvs %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 1, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cvs <- omc_cvs %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

names(omc_cvs)[1]<-"id_event"
names(omc_cvs)[2]<-"id_related_case"

names(omc_cvs)[11]<-"GrupoArmado1"
names(omc_cvs)[12]<-"GrupoArmado1_name"

omc_cvs <- omc_cvs %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         deaths_total_combatants = NA,
         deaths_total_civilians = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cvs <- omc_cvs %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cvs %>%
  group_by(GrupoArmado1) %>%
  count()

omc_cvs <- omc_cvs %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cvs <- omc_cvs %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cvs <- omc_cvs %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cvs <- omc_cvs %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cvs <- omc_cvs %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cvs <- omc_cvs %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

### Attacks Civilian Property ###

omc_cdb <- read_xlsx("CasosDB_202309.xlsx")

# Reshape the dataset

omc_cdb <- omc_cdb %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cdb <- omc_cdb %>%
  dplyr::filter(year != 0)
omc_cdb <- omc_cdb %>%
  dplyr::filter(divipola_alt != "00000")

omc_cdb <- omc_cdb %>%
  mutate(target = "2. Civilian")

omc_cdb <- omc_cdb %>%
  mutate(event_type = "2. Unilateral Action")

omc_cdb <- omc_cdb %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cdb <- omc_cdb %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 1, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cdb <- omc_cdb %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

names(omc_cdb)[1]<-"id_event"
names(omc_cdb)[2]<-"id_related_case"

names(omc_cdb)[11]<-"GrupoArmado1"
names(omc_cdb)[12]<-"GrupoArmado1_name"

omc_cdb <- omc_cdb %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         deaths_total_combatants = NA,
         deaths_total_civilians = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cdb <- omc_cdb %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cdb <- omc_cdb %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cdb <- omc_cdb %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cdb <- omc_cdb %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cdb <- omc_cdb %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cdb <- omc_cdb %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cdb <- omc_cdb %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

### Terrorist Attacks Against Civilian Objectives ###

omc_cat <- read_xlsx("CasosAT_202309.xlsx")

# Reshape the dataset

omc_cat <- omc_cat %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                mun_uc = Municipio,
                dep_uc = Departamento)

names(omc_cat)[1]<-"id_event"
names(omc_cat)[2]<-"id_related_case"

names(omc_cat)[6]<-"divipola_alt"
names(omc_cat)[11]<-"GrupoArmado1"
names(omc_cat)[24]<-"deaths_total_combatants"
names(omc_cat)[25]<-"deaths_total_civilians"

names(omc_cat)[12]<-"GrupoArmado1_name"

omc_cat$divipola_alt <- as.character(omc_cat$divipola_alt)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cat <- omc_cat %>%
  dplyr::filter(year != "0000")
omc_cat <- omc_cat %>%
  dplyr::filter(divipola_alt != "00000")

omc_cat %>%
  group_by(Modalidad) %>%
  count()

omc_cat_cobj <- omc_cat %>%
  dplyr::filter(Modalidad == "ATAQUE A OBJETIVO MILITAR CON EFECTO INDISCRIMINADO")

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(target = "2. Civilian")

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(event_type = "2. Unilateral Action")

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(clash = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0, # Incursion y Ataque a Población
         combatant_killed = 0, # Combatiente Muerto en Circunstancia no Determinada 
         clash_2 = 0)

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 1, # Ataque Terrorista contra Objetivo Civil
  )

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0, # Ataque Terrorista contra Objetivo Ambiguo
  )

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado1_name = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

# Create temporal variables to reduce complexity
##############################################################################
omc_cat_cobj <- omc_cat_cobj %>%
  mutate(del_ga = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO"
    | GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))
##############################################################################

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTES DEL ESTADO" ~ 1,
    TRUE ~ 0))

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" ~ 1,
    TRUE ~ 0))

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(other_ag = case_when(
    del_ga == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(unident_ag = case_when(
    del_ga == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cat_cobj <- omc_cat_cobj %>%
  mutate(unknown = case_when(
    del_ga == "Unknown" ~ 1,
    TRUE ~ 0))

omc_cat_cobj <- omc_cat_cobj %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

## Now, I will merge all the datasets related to civilian targets.

omc_cas <- omc_cas %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cma <- omc_cma %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cdf <- omc_cdf %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cse <- omc_cse %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cvs <- omc_cvs %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cdb <- omc_cdb %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cat_cobj <- omc_cat_cobj %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage, Modalidad)

omc_cco <- dplyr::bind_rows(omc_cas, omc_cma)
omc_cco <- dplyr::bind_rows(omc_cco, omc_cdf)
omc_cco <- dplyr::bind_rows(omc_cco, omc_cse)
omc_cco <- dplyr::bind_rows(omc_cco, omc_cvs)
omc_cco <- dplyr::bind_rows(omc_cco, omc_cdb)
omc_cco <- dplyr::bind_rows(omc_cco, omc_cat_cobj)

str(omc_cco)

omc_cco <- omc_cco %>%
  dplyr::filter(year != "0000")

### Prepare the final integrated dataset on conflict events with military objectives

omc_cco_dep <- omc_cco %>% distinct(id_event, year, month, day, divipola_alt, .keep_all = TRUE)

write_csv(omc_cco, "~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)_OMC-CasosFinal_CivilianTarget-Updated(09132024).csv") 

########################################################################
########################################################################
# Creating the Dataset on Conflict Events
### Ambiguous Targets
########################################################################
########################################################################

omc_cat <- read_xlsx("CasosAT_202309.xlsx")

# Reshape the dataset

omc_cat <- omc_cat %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                mun_uc = Municipio,
                dep_uc = Departamento)

names(omc_cat)[1]<-"id_event"
names(omc_cat)[2]<-"id_related_case"

names(omc_cat)[6]<-"divipola_alt"
names(omc_cat)[11]<-"GrupoArmado1"
names(omc_cat)[24]<-"deaths_total_combatants"
names(omc_cat)[25]<-"deaths_total_civilians"

names(omc_cat)[12]<-"GrupoArmado1_name"

omc_cat$divipola_alt <- as.character(omc_cat$divipola_alt)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cat <- omc_cat %>%
  dplyr::filter(year != "0000")
omc_cat <- omc_cat %>%
  dplyr::filter(divipola_alt != "00000")

omc_cat %>%
  group_by(Modalidad) %>%
  count()

omc_cat_aobj <- omc_cat %>%
  dplyr::filter(Modalidad == "DESCONOCIDA")

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(target = "3. Ambiguous")

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(event_type = "2. Unilateral Action")

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(clash = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0, # Incursion y Ataque a Población
         combatant_killed = 0, # Combatiente Muerto en Circunstancia no Determinada 
         clash_2 = 0)

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0, # Ataque Terrorista contra Objetivo Civil
  )

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(map_muse = 0, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 1, # Ataque Terrorista contra Objetivo Ambiguo
  )

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado1_name = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

# Create temporal variables to reduce complexity
##############################################################################
omc_cat_aobj <- omc_cat_aobj %>%
  mutate(del_ga = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO"
    | GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude"))
##############################################################################

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTES DEL ESTADO" ~ 1,
    TRUE ~ 0))

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" ~ 1,
    TRUE ~ 0))

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(other_ag = case_when(
    del_ga == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(unident_ag = case_when(
    del_ga == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cat_aobj <- omc_cat_aobj %>%
  mutate(unknown = case_when(
    del_ga == "Unknown" ~ 1,
    TRUE ~ 0))

omc_cat_aobj <- omc_cat_aobj %>%
  mutate_at(vars(state_forces, rebel_groups, progov_militias, other_ag,
                 unident_ag, unknown), ~replace_na(., 0)) 

### Mines ###

omc_cmi <- read_xlsx("CasosMI_202309.xlsx")

# Reshape the dataset

omc_cmi <- omc_cmi %>%
  dplyr::rename(year = Año,
                month = Mes,
                day = Día,
                divipola_alt = "Código DANE de Municipio",
                mun_uc = Municipio,
                dep_uc = Departamento)

# Delete unclear categories and events without basic information about year and/or municipality.
omc_cmi <- omc_cmi %>%
  dplyr::filter(year != "0000")
omc_cmi <- omc_cmi %>%
  dplyr::filter(divipola_alt != "00000")

omc_cmi <- omc_cmi %>%
  mutate(target = "3. Ambiguous")

omc_cmi <- omc_cmi %>%
  mutate(event_type = "2. Unilateral Action")

omc_cmi <- omc_cmi %>%
  mutate(clash = 0, # Combate
         clash_2 = 0, # Combate
         air_attack = 0, # Ataque Militar desde el Aire (Ametrallamiento o Bombardeo)
         mil_operation = 0, # Operativo Militar
         tatt_mil_target = 0, # Ataque a Instalación de las FUERZAS ARMADAS ESTATALES o Ataque Terrorista a Objetivo Militar
         ambush = 0, # Emboscada
         hitnrun = 0, # Hostigamiento
         raid = 0
  )

omc_cmi <- omc_cmi %>%
  mutate(assassination = 0, # Asesinato
         massacre = 0, # Massacre
         f_disapp = 0, # Desaparición Forzada
         ind_kidn = 0, # Secuestro Individual
         col_kidn = 0, # Secuestro Colectivo & Secuestro Múltiple
         sex_v = 0, # Violencia Sexual
         attack_civp = 0, # Ataque a Bienes Civiles
         tatt_civ_target = 0 # Terrorist Attack against Civilian Targets
  )

omc_cmi <- omc_cmi %>%
  mutate(map_muse = 1, # Accidente/Ataque con MAP or MUSE
         tatt_amb_target = 0 # Terrorist Attack against Ambiguous Targets
  )

names(omc_cmi)[1]<-"id_event"
names(omc_cmi)[2]<-"id_related_case"

names(omc_cmi)[11]<-"GrupoArmado1"
names(omc_cmi)[12]<-"GrupoArmado1_name"

names(omc_cmi)[24]<-"deaths_total_combatants"
names(omc_cmi)[25]<-"deaths_total_civilians"

omc_cmi <- omc_cmi %>%
  mutate(captured_name_group = NA,
         captured_nonstate = NA,
         deaths_military = NA,
         deaths_police = NA, 
         deaths_other_state = NA, 
         deaths_unknown_state = NA,
         deaths_state_forces = NA,
         deaths_rebels = NA,
         deaths_pgm = NA,
         deaths_gpdp = NA,
         deaths_unknown_groups = NA,
         deaths_other_groups = NA,
         deaths_nonstate_groups = NA,
         deaths_total_noinfo = NA,
         GrupoArmado2 = NA,
         GrupoArmado3 = NA,
         GrupoArmado2_name = NA,
         GrupoArmado3_name = NA,
         Iniciativa = NA,
         military_advantage = NA)

##############################################################################
omc_cmi <- omc_cmi %>%
  mutate(del_ga1 = case_when(
    GrupoArmado1 == "AGENTE EXTRANJERO" | GrupoArmado1 == "BANDOLERISMO" |
      GrupoArmado1 == "CRIMEN ORGANIZADO" 
    | GrupoArmado1 == "GRUPO POSDESMOVILIZACIÓN" ~ "Other Group",
    GrupoArmado1 == "GRUPO ARMADO NO DIRIMIDO" | GrupoArmado1 == "GRUPO ARMADO NO IDENTIFICADO" ~ "Unidentified Group",
    GrupoArmado1 == "DESCONOCIDO" | GrupoArmado1 == "OTRO ¿CUÁL?" ~ "Unknown",
    TRUE ~ "Exclude")) # Double check the purpose of 'Exclude'
##############################################################################

omc_cmi <- omc_cmi %>%
  mutate(state_forces = case_when(
    GrupoArmado1 == "AGENTE DEL ESTADO" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO POSDESMOVILIZACIÓN"
    ~ 1,
    TRUE ~ 0))

omc_cmi <- omc_cmi %>%
  mutate(rebel_groups = case_when(
    GrupoArmado1 == "GUERRILLA" ~ 1,
    TRUE ~ 0))

omc_cmi <- omc_cmi %>%
  mutate(progov_militias = case_when(
    GrupoArmado1 == "GRUPO PARAMILITAR" |
      GrupoArmado1 == "AGENTE DEL ESTADO - GRUPO PARAMILITAR"  ~ 1,
    TRUE ~ 0))

omc_cmi <- omc_cmi %>%
  mutate(other_ag = case_when(
    del_ga1 == "Other Group" ~ 1,
    TRUE ~ 0))

omc_cmi <- omc_cmi %>%
  mutate(unident_ag = case_when(
    del_ga1 == "Unidentified Group" ~ 1,
    TRUE ~ 0))

omc_cmi <- omc_cmi %>%
  mutate(unknown = case_when(
    del_ga1 == "Unknown" ~ 1,
    TRUE ~ 0))

omc_cmi <- omc_cmi %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cat_aobj <- omc_cat_aobj %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_aco <- dplyr::bind_rows(omc_cmi, omc_cat_aobj)


omc_aco_dep <- omc_aco %>% distinct(id_event, year, month, day, divipola_alt, .keep_all = TRUE)

write_csv(omc_aco, "~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)_OMC-CasosFinal_AmbiguousTarget-Updated(09132024).csv") 

########################################################################
########################################################################
# Creating the Dataset on Conflict Events
### Merging all datasets
########################################################################
########################################################################

omc_cmt <- read_csv("~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)-OMC-CasosFinal_MilitaryTarget-Updated(09132024).csv")
omc_cct <- read_csv("~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)_OMC-CasosFinal_CivilianTarget-Updated(09132024).csv")
omc_cat <- read_csv("~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)_OMC-CasosFinal_AmbiguousTarget-Updated(09132024).csv")

omc_cmt <- omc_cmt %>%
  select(id_event, id_related_case, year, month, day, 
         divipola_alt, mun_uc, dep_uc,
         target, event_type,
         clash, clash_2, air_attack, mil_operation, tatt_mil_target, ambush, hitnrun, raid,
         assassination, massacre, f_disapp, ind_kidn, col_kidn, sex_v, attack_civp, tatt_civ_target,
         map_muse, tatt_amb_target,
         state_forces, rebel_groups, progov_militias, other_ag, unident_ag, unknown,
         captured_nonstate, captured_name_group,
         deaths_military, deaths_police, deaths_other_state, deaths_unknown_state, 
         deaths_state_forces, deaths_rebels, deaths_pgm, deaths_gpdp,
         deaths_unknown_groups, deaths_other_groups, deaths_nonstate_groups, 
         deaths_total_combatants, deaths_total_noinfo, deaths_total_civilians,
         GrupoArmado1, GrupoArmado1_name, GrupoArmado2, GrupoArmado2_name, GrupoArmado3, GrupoArmado3_name,
         Iniciativa, military_advantage)

omc_cct <- omc_cct %>%
  select(-Modalidad)

omc_cf <- dplyr::bind_rows(omc_cmt, omc_cct)
omc_cf <- dplyr::bind_rows(omc_cf, omc_cat)

omc_cf <- omc_cf %>%
  dplyr::filter(year != 0)
omc_cf <- omc_cf %>%
  dplyr::filter(divipola_alt != "00000")
omc_cf <- omc_cf %>%
  dplyr::filter(divipola_alt != "0")
omc_cf <- omc_cf %>%
  dplyr::filter(divipola_alt != "00000")

write_csv(omc_cf, "~/Library/Mobile Documents/com~apple~CloudDocs/Documents/Sam Houston State University/1.ResearchAgenda&Pipeline/Project3/RegionalData/OMC-Data_Updated-Products(2024)/Ortega(2024)-OMC-CasosFinal_Aggregated(09142024).csv")
